<?php

class ThumbController extends Zend_Controller_Action
{

	public function __call($methodName, $args)
	{
		$model = new Default_Model_Upload();
		$path = $this->_request->getPathInfo();
		$mimetype = Default_Model_Upload::$mime['gif'];
		$thumbnail = '../css/space.gif';
		if (preg_match('@^/thumb/(\d+)x(\d+)([^/]*)/([^\.]+)\.(jpg|png|gif)$@i', $path, $match))
		{
			$filename = Default_Model_Upload::DIR_UPLOAD."{$match[4]}.{$match[5]}";
			$cachedir = Default_Model_Upload::DIR_THUMB."{$match[1]}x{$match[2]}{$match[3]}";
			if (is_readable($filename) && is_writable($cachedir))
			{
				$ext = strtolower($match[5]);
				$mimetype = Default_Model_Upload::$mime[$ext];
				$thumbnail = $model->createPath("{$match[4]}.{$match[5]}", $cachedir);
				if (!file_exists($thumbnail))
				{
					$model->createThumbnail($filename, $thumbnail, $match[1], $match[2], $match[3]);
					chmod($thumbnail, 0664);
				}
			}
		}
		$model->flushFile($thumbnail, $mimetype);
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
	}
	
}